﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities.Resumes;
using Forest.Core;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;

namespace Forest.Cat.Domain.Services.Resumes
{
    public class ShieldedCompanyService : IShieldedCompanyService
    {
        #region Constants
        private const string ShieldedCompany_BY_ID_KEY = "shieldedCompany.id-{0}";
        private const string ShieldedCompany_PATTERN_KEY = "shieldedCompany.";
        #endregion

        #region Fields
        private readonly IRepository<ShieldedCompany> _shieldedCompanyRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;

        #endregion

        #region Ctor
        public ShieldedCompanyService(IRepository<ShieldedCompany> workExperienceRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _shieldedCompanyRepository = workExperienceRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteShieldedCompany(ShieldedCompany shieldedCompany)
        {
            if (shieldedCompany == null)
                throw new ArgumentNullException("ShieldedCompany");

            _shieldedCompanyRepository.Delete(shieldedCompany);

            _cacheManager.RemoveByPattern(ShieldedCompany_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(shieldedCompany);

        }

        public ShieldedCompany GetShieldedCompanyById(Guid shieldedCompanyId)
        {
            if (shieldedCompanyId == Guid.Empty)
                return null;

            string key = string.Format(ShieldedCompany_BY_ID_KEY, shieldedCompanyId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _shieldedCompanyRepository.GetById(shieldedCompanyId);
                return cl;
            });
        }

        public virtual IPagedList<ShieldedCompany> GetAllShieldedCompanys(int pageIndex, int pageSize)
        {
            var query = _shieldedCompanyRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<ShieldedCompany>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertShieldedCompany(ShieldedCompany shieldedCompany)
        {
            if (shieldedCompany == null)
                throw new ArgumentNullException("ShieldedCompany");

            _shieldedCompanyRepository.Insert(shieldedCompany);

            _cacheManager.RemoveByPattern(ShieldedCompany_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(shieldedCompany);
        }

        public virtual void UpdateShieldedCompany(ShieldedCompany shieldedCompany)
        {
            if (shieldedCompany == null)
                throw new ArgumentNullException("ShieldedCompany");
            _shieldedCompanyRepository.Update(shieldedCompany);

            _cacheManager.RemoveByPattern(ShieldedCompany_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(shieldedCompany);
        }

        public IQueryable<ShieldedCompany> GetShieldedCompanyByJobseekerId(Guid id)
        {
            return _shieldedCompanyRepository.Table.Where(w => w.JobseekerId == id);
        }

        #endregion



    }
}
